package com.example.entity;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

import static com.example.util.FileEncryptionUtil.decryptFile;
import static com.example.util.ProcessShowCommander.showFileContent;

public class Server {
    public static void main(String[] args) {
        try {
            ServerSocket serverSocket = new ServerSocket(8888);
            System.out.println("服务器已启动，等待客户端连接...");

            while (true) {
                Socket clientSocket = serverSocket.accept();
                System.out.println("客户端连接成功：" + clientSocket.getInetAddress().getHostAddress());

                // 接收文件
                InputStream inputStream = clientSocket.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
                String encryptedFile = reader.readLine();

                // 解密文件
                String decryptedFile = decryptFile(encryptedFile);

                System.out.println("文件接收并解密完成：" + decryptedFile);

                // 处理show命令
                String cmd = reader.readLine();
                if (cmd.startsWith("show ")) {
                    String filePath = cmd.substring(5);
                    showFileContent(filePath);
                }

                clientSocket.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}